package action;

import com.google.code.kaptcha.Constants;
import entity.Account;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import service.AccountService;
import service.impl.AccountServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;

@Slf4j
@WebServlet("/AccountServlet")
public class AccountAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AccountService accountService = new AccountServiceImpl();

	public AccountAction() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("method");
		method = method != null && method.length() > 0 ? method : "login";
		switch (method) {
		case "login":
			response.sendRedirect("login.jsp");
			break;
		case "loginConfig":
			loginConfig(request, response);
			break;
		case "createConfig":
			createConfig(request, response);
			break;
		case "logoutConfig":
			logoutConfig(request, response);
			break;
		case "ajax" :
			ajax(request, response);
			break;
		default:
			response.sendRedirect("login.jsp");
			break;
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
	
	protected void ajax(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String username = request.getParameter("username");
		log.info("username================>"+username);
		try {
			Account account = accountService.exist(Account.builder().username(username).build());
			if (account != null) {
				out.print("true");
			} else {
				out.print("false");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		out.close();
	}

	private void loginConfig(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String code = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
		try {
			Account account = new Account();
			Map<String,String[]> map = request.getParameterMap();
			BeanUtils.populate(account,map);

			if (!account.getCode().equals(code)) {
				request.setAttribute("codeFail", "验证码输入错误");
				request.getRequestDispatcher("login.jsp").forward(request, response);
				return;
			}

			account = accountService.exist(account);

			if (account != null) {
				HttpSession session = request.getSession();
				session.setAttribute("username", account.getUsername());
				if (account.getSave() != null && account.getSave().equals("yes")) {
					Cookie username_cookie = new Cookie("username", account.getUsername());
					Cookie password_cookie = new Cookie("password", account.getPassword());
					Cookie save_cookie = new Cookie("save", account.getSave());
					username_cookie.setMaxAge(1800);
					password_cookie.setMaxAge(1800);
					save_cookie.setMaxAge(1800);
					response.addCookie(username_cookie);
					response.addCookie(password_cookie);
					response.addCookie(save_cookie);
				} else {
					Cookie username_cookie = new Cookie("username", "");
					Cookie password_cookie = new Cookie("password", "");
					Cookie save_cookie = new Cookie("save", "");
					response.addCookie(username_cookie);
					response.addCookie(password_cookie);
					response.addCookie(save_cookie);
				}
				response.sendRedirect("TouristServlet");
			} else {
				request.setAttribute("fail", "密码输入错误");
				request.getRequestDispatcher("login.jsp").forward(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void createConfig(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		try {
			Map<String,String[]> map = request.getParameterMap();
			Account account = new Account();
			BeanUtils.populate(account,map);
			account.setCreated_time(new Date());
			log.info("{}",account);
			accountService.insert(account);
		} catch (Exception e) {
			e.printStackTrace();
		}
		request.getRequestDispatcher("login.jsp").forward(request,response);

	}

	protected void logoutConfig(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		session.invalidate();
		response.sendRedirect("login.jsp");
	}
}